/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package k3tool.opf.model;

import java.io.File;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 *
 * @author Jeff Huang
 */
public abstract class KindleResourceTypeHelper {

    private static final Logger logger = Logger.getLogger(KindleResourceTypeHelper.class.getName());
    public static final HashMap<KindleResourceType, String> typeNameMap =
            new HashMap<KindleResourceType, String>();
    public static final HashMap<String, KindleResourceType> suffixTypeMap =
            new HashMap<String, KindleResourceType>();

    static {
        typeNameMap.put(KindleResourceType.xhtml, "application/xhtml+xml");
        typeNameMap.put(KindleResourceType.gif, "image/gif");
        typeNameMap.put(KindleResourceType.png, "image/png");
        typeNameMap.put(KindleResourceType.jpeg, "image/jpeg");

        suffixTypeMap.put("html", KindleResourceType.xhtml);
        suffixTypeMap.put("htm", KindleResourceType.xhtml);
        suffixTypeMap.put("xhtml", KindleResourceType.xhtml);
        suffixTypeMap.put("jpg", KindleResourceType.jpeg);
        suffixTypeMap.put("jpeg", KindleResourceType.jpeg);
        suffixTypeMap.put("gif", KindleResourceType.gif);
        suffixTypeMap.put("png", KindleResourceType.png);

    }

    /**
     * To determine whether an item could be used as a spine.
     *
     * This method will be use to generate the spine items or TOC.
     * 
     * @param file
     * @return
     */
    public static boolean couldBeSpineItem(File file) {
        String suffix = file.getName().toLowerCase();
        int pos = suffix.lastIndexOf(".");
        if (pos == -1) {
            return false;
        }
        suffix = suffix.substring(pos + 1);
        KindleResourceType type = KindleResourceTypeHelper.suffixTypeMap.get(suffix);
        if (type == null) {
            logger.info("Unsupport type : " + file.getAbsolutePath());
            return false;
        }
        if (type != KindleResourceType.xhtml) {
            return false;
        }
        return true;
    }
}
